export function getAvailable(value) {
    const availableElement = document.querySelector(
        ".mantine-1jhdd3r > .tr-font-medium"
    );
    console.log(availableElement, "avaaaaaaaaaaaaaaaaaaaa");
    if (availableElement) {
        let dataList = availableElement.innerHTML.split(" ");
        availableElement.innerText = `${value.toFixed(2)} ${dataList[1]}`;
        console.log(availableElement.innerText, "availableElement.innerText");
    } else {
        console.log("未找到.mantine-1jhdd3r > .tr-font-medium节点");
    }
}

export function getAlphaBuyOrSell() {
    const btn =
        document.querySelector(".mantine-16qpx8y > .mantine-lvhsao") ||
        document.querySelector('.mantine-16qpx8y > .mantine-1a7amj5');
    console.log(btn, "btn");
    const buySellBtn = document.querySelector(".mantine-5wozre > .kNqabE");
    console.log(buySellBtn, "buySellBtn");
    if (buySellBtn) {
        if (buySellBtn.innerText == "买入") {
            getNumValue();
        } else {
            getTurnoverValue();
        }
    } else {
        console.log("未找到.mantine-5wozre > .kNqabE买入按钮节点");
    }

    if (btn) {
        const originalHandler = btn.onclick;
        if (originalHandler) {
            btn.onclick = null;
        }
        btn.addEventListener("click", async (event) => {
            console.log("买入按钮被点击");
            event.preventDefault();
            console.log("执行自定义逻辑");
            // chrome.runtime.sendMessage(
            //   {
            //     action: "interceptRequest",
            //     url: "https://www.gate.io/apiw/v2/unified/futures/usdt/contracts",
            //   },
            //   (response) => {
            //     if (chrome.runtime.lastError) {
            //       console.error("消息发送失败：", chrome.runtime.lastError.message);
            //     } else {
            //       console.log("收到响应：", response);
            //     }
            //   }
            // );
        });
    } else {
        console.log("未找到.mantine-16qpx8y > .mantine-lvhsao买入按钮节点");
    }
}

function getTurnoverValue() {
    const turnoverElement = document.querySelector(
        '.mantine-Input-wrapper > input[placeholder="交易额"]'
    );
    console.log(turnoverElement, "turnovryeeeee");
    if (turnoverElement) {
        console.log(turnoverElement.value);
    } else {
        console.log(
            '未找到.mantine-Input-wrapper > input[placeholder="交易额"]交易额输入框'
        );
    }
}

function getNumValue() {
    const numElement = document.querySelector(
        '.mantine-Input-wrapper > input[placeholder="数量"]'
    );
    console.log(numElement, "numElement");
    if (numElement) {
        console.log(numElement.value);
    } else {
        console.log(
            '未找到.mantine-Input-wrapper > input[placeholder="数量"]数量输入框'
        );
    }
}
